<template>
  <div class="list">
    <el-table :data="students" style="width: 100%">
      <el-table-column prop="id" label="学号"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="text" @click="lookClick(scope.$index)" size="small"
            >查看</el-button
          >
          <el-button type="text" @click="editClick(scope.$index)" size="small"
            >编辑</el-button
          >
          <el-button type="text" @click="delClick(scope.$index)" size="small"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="button" @click="dialogFormVisibleAdd = true"
      >添加学生</el-button
    >

    <div>
         <el-dialog title="查看" :visible.sync="dialogFormVisible">
  <el-form :model="activeStudent" :disabled="true">
    <el-form-item label="学号" :label-width="formLabelWidth">
      <el-input v-model="activeStudent.id" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="姓名" :label-width="formLabelWidth">
      <el-input v-model="activeStudent.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="性别" :label-width="formLabelWidth">
      <el-select v-model="activeStudent.gender" >
        <el-option label="男" value="M"></el-option>
        <el-option label="女" value="F"></el-option>
      </el-select>
       
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
  </div>
</el-dialog>
    </div>

      <div>
         <el-dialog title="编辑" :visible.sync="dialogFormVisibleUp">
  <el-form :model="editingStudent">
    <el-form-item label="学号" :label-width="formLabelWidth">
      <el-input v-model="editingStudent.id" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="姓名" :label-width="formLabelWidth">
      <el-input v-model="editingStudent.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="性别" :label-width="formLabelWidth">
      <el-select v-model="editingStudent.gender" >
        <el-option label="男" value="M"></el-option>
        <el-option label="女" value="F"></el-option>
      </el-select>
       
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisibleUp = false">取消</el-button>
    <el-button type="primary" @click="updataClick">确 定</el-button>
  </div>
</el-dialog>
    </div>

    <div>
         <el-dialog title="增加" :visible.sync="dialogFormVisibleAdd">
  <el-form :model="newStudent">
    <el-form-item label="学号" :label-width="formLabelWidth">
      <el-input v-model="newStudent.id" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="姓名" :label-width="formLabelWidth">
      <el-input v-model="newStudent.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="性别" :label-width="formLabelWidth">
      <el-select v-model="newStudent.gender" >
        <el-option label="男" value="M"></el-option>
        <el-option label="女" value="F"></el-option>
      </el-select>
       
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisibleAdd = false">取消</el-button>
    <el-button type="primary" @click="addClick">确 定</el-button>
  </div>
</el-dialog>
    </div>
  </div>
</template>
<script>
import { studentsData } from "../../../public/schoolData.js";
export default {
    data(){
        return{
            students:studentsData,
            handelIndex:-1,
            formLabelWidth:"100px",
            dialogFormVisible:false,
            dialogFormVisibleUp:false,
            dialogFormVisibleAdd:false,
            editingStudent:{},
            newStudent:{
                id:"",
                name:"",
                gender:""
            }
        }
    },
    computed:{
        activeStudent(){
            return this.handelIndex>=0?this.students[this.handelIndex]:{}
        }
    },
    methods:{
        lookClick(index){
            this.dialogFormVisible=true,
            this.handelIndex=index
        },
        editClick(index){
             this.dialogFormVisibleUp=true,
            this.handelIndex=index
            this.editingStudent={...this.activeStudent}
        },
        updataClick(){
            this.students= this.students.map((student,index)=>this.handelIndex==index?this.editingStudent:student)
             this.dialogFormVisibleUp=false
        },

        delClick(index){
            this.students.splice(index,1)
        },

        addClick(){
            this.students.push({...this.newStudent,id:Date.now()})
             this.dialogFormVisibleadd=false

        }
    }
}
</script>

<style scoped>
</style>